package com.anpai.main;

import com.anpai.entity.UserBean;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.anpai.main.LoginJFrame.*;


public class CheckMessage extends JFrame implements ActionListener {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static JLabel surplusTime;

    public static UserBean userBean = null;
    @Override
    public void actionPerformed(ActionEvent e) {

    }

    private static JLabel getSurplusTime() {
        if (surplusTime == null) {
            surplusTime = new JLabel("");
            surplusTime.setBounds(82, 88, 100, 50);
            surplusTime.setFont(new Font("微软雅黑", Font.BOLD, 12));
            String time0 = "00:20:00";
            String strMsg = "<html><body>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;考试时间："+time0+"</body></html>";
            surplusTime.setText(strMsg);
        }
        return surplusTime;
    }


    static class MyLayout extends JFrame implements ActionListener{

        JLabel jLabelTime = new JLabel();
        JButton reallyBt = new MyJButton("开始考试");
        JButton backBt = new MyJButton("注销");
        JButton exitBt = new MyJButton("退出");

        public MyLayout(){
            this.setTitle("考试系统-考场纪律");
            setLocation(10, 10);
            setBounds(550,300, 800, 540);
            setLayout(new BorderLayout(10,10)); // 上下左右间距10

            // 顶部部分
            JPanel headJPanel = new JPanel();
            backBt.addActionListener(this);
            reallyBt.addActionListener(this);
            exitBt.addActionListener(this);

            headJPanel.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();

            gbc.gridx = 1;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.weightx = 1;
            gbc.weighty = 1;
            headJPanel.add(getSurplusTime(), gbc);

            gbc.gridx = 2;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.weightx = 1;
            gbc.weighty = 1;
            headJPanel.add(reallyBt, gbc);

            gbc.gridx = 3;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.weightx = 1;
            gbc.weighty = 1;
            headJPanel.add(backBt, gbc);

            gbc.gridx = 4;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.weightx = 1;
            gbc.weighty = 1;
            headJPanel.add(exitBt, gbc);


            add(headJPanel, BorderLayout.NORTH);

            // 中间部分
            JPanel centerJPanel = new JPanel();

            JLabel jl = new JLabel("<html><h1>考场纪律</h1></html>",SwingConstants.CENTER);
            // 考场纪律
            JTextArea jTextArea = new JTextArea(20,65);
            jTextArea.setLineWrap(true);        //激活自动换行功能
            jTextArea.setWrapStyleWord(true);            // 激活断行不断字功能
            jTextArea.setEditable(false);
            String txtMessage =
                    "1、所有考生在考场内手机一律保持关机状态，手机未关机的一经发现以作弊论处。\n\n" +
                    "2、考生的所有与考试有关的复习材料和书本一律不得带入考场，书本有带入考场的不论是否有看，均以作弊论处。考生书本可以放在五楼考场门口的蓝色塑料箱内，考完试后到出口处领回。\n\n" +
                    "3、考生在进场前请将准考证和身一份一证拿在手上，有照片的面朝上以便考官核对身份。\n\n" +
                    "4、考生进场后，径直往前走，听从考官安排，并按考官安排的位置就做，考生就做后就可以开始动手操作电脑进行考试，用右手食指按指模后确认本人信息后开始考试，整个考试过程考生应自己独立操作。\n\n" +
                    "5、考生答题完毕后，点击交卷键。交卷后电脑会立刻评分，分数在90分以上的学员包括90分考试合格，不需要再对电脑进行任何操作，更无需举手 示意，自行离开考场，不需要领取成绩单，成绩单由学校带队统一领取，分数在89分以下的考试不合格，请坐在座位上等待10秒钟，电脑会自动回到原始界面，考生可重新按指模补考一次。如果两次考试都不合格的考生请从出口处退出考场。考场在入口处有写“入口”，出口处有写“出口”两字，中间过道有三面“出口” 的标志牌，标明出口方向。考生离场时，请注意观察。工作人员会将书本从“入口”处搬到“出口”处，考生请到出口处拿回自己的书本。\n\n" +
                    "6、考生在考场内严禁吸烟、吃零食，尤其是有吃口香糖的学员，请在进场前将口香糖吐在侯考区的垃圾桶内。7、考生按指纹时应该用在驾校采集指纹时的手指，忘记的考生请用右手食指。按指纹失败的点击确定后重新再按直到学员信息出来既可开始考试，按指纹不限次数。指纹不行，需要考官授权考试的考生在考试结束后要回到驾校重新采集指纹。";
            jTextArea.append(txtMessage);
            JScrollPane scroll = new JScrollPane(jTextArea);
            //把定义的JTextArea放到JScrollPane里面去
            //分别设置水平和垂直滚动条自动出现
            scroll.setHorizontalScrollBarPolicy(
                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            scroll.setVerticalScrollBarPolicy(
                    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

            centerJPanel.add(jl);
            centerJPanel.add(scroll);

            add(centerJPanel);



            currenttime ct=new currenttime();
            ct.start();
            // 底部部分
           // String userName = userTxt.getText();
            String key = null;
            Iterator it = userMap.keySet().iterator();
            while (it.hasNext()) {
                key = (String) it.next();
                if(userTxt.getText().equals(key)){
                    userBean = (UserBean) userMap.get(key);
                    break;
                }
            }


            JPanel bottomJPanel = new JPanel();
            bottomJPanel.setBackground(Color.CYAN);
            JLabel jLabelName = new JLabel("姓名："+userBean.getName());
            JLabel jLabelNumber= new JLabel("学号："+userBean.getStudentNo());
            JLabel jLabelSubject= new JLabel("科目：心理测试");
            JLabel jLabelTopics= new JLabel("题目数量：20");

            bottomJPanel.add(jLabelName);
            bottomJPanel.add(jLabelNumber);
            bottomJPanel.add(jLabelSubject);
            bottomJPanel.add(jLabelTopics);
            bottomJPanel.add(jLabelTime);

            add(bottomJPanel, BorderLayout.SOUTH);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
        }

        // 动态时间创建
        class currenttime extends Thread
        {
            Date datedisplay;
            GregorianCalendar gccalendar;
            String strtime;
            public currenttime(){}
            public void run()
            {
                while(true)
                {
                    displaytime();
                    try
                    {
                        this.sleep(1000);
                    }
                    catch(InterruptedException e)
                    {
                        JOptionPane.showMessageDialog(null,"线程中断");
                    }
                }
            }
            public void displaytime()
            {
                datedisplay=new Date();
                gccalendar=new GregorianCalendar();
                gccalendar.setTime(datedisplay);
                strtime= "当前时间："+dateChange(gccalendar.getTime(),"yyyy年MM月dd日 hh:mm:ss");
                jLabelTime.setText(strtime);
            }

            public String dateChange(Date date, String pattern) {
                SimpleDateFormat sdf = new SimpleDateFormat(pattern);
                String demo = sdf.format(date);
                return demo;
            }
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == exitBt){
                System.exit(0);
            }
            if(e.getSource() == backBt){
                this.setVisible(false);
                new LoginJFrame();
            }
            if(e.getSource() == reallyBt){
                // 获取题库
                titlesPath = path + "\\src\\main\\java\\com\\anpai\\data\\心理测试题.xls";
                try {
                    getTitles();
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                }
                this.setVisible(false);
                new ExamJFrame.MyLayout();
            }
        }

    }

}
